package org.example.redisdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyController {
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/testString")
    @ResponseBody
    public String testString() {
        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");
        String value = (String) redisTemplate.opsForValue().get("key");
        System.out.println(value);
        return "ok";
    }

    @RequestMapping("/testList")
    @ResponseBody
    public String testList() {
        //清楚之前的数据
        redisTemplate.execute((RedisConnection redisConnection)->{
            //excute 要求回调方法中必须写 return语句，返回个东西
            //这个回调返回的对象，就会作为excute本身的返回值。
            redisConnection.flushAll();
            return null;
        });
        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "222");
        redisTemplate.opsForList().leftPush("key", "333");
        String value = (String) redisTemplate.opsForList().leftPop("key");
        System.out.println(value);
        value = (String) redisTemplate.opsForList().leftPop("key");
        System.out.println(value);

        return "ok";
    }

    @RequestMapping("/testSet")
    @ResponseBody
    public String testSet(){
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key","111","222","333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println("result:"+result);

        Boolean exists = redisTemplate.opsForSet().isMember("key","111");
        System.out.println("exists:"+exists);

        long count = redisTemplate.opsForSet().size("key");
        System.out.println("count:"+count);

        redisTemplate.opsForSet().remove("key","111","222");
        result = redisTemplate.opsForSet().members("key");
        System.out.println(result);
        return "ok";
    }

    @RequestMapping("/testHash")
    @ResponseBody
    public String testHash(){
        redisTemplate.execute((RedisConnection connection)->{
           connection.flushAll();
           return null;
        });
        redisTemplate.opsForHash().put("key","f1","111");
        redisTemplate.opsForHash().put("key","f2","222");
        redisTemplate.opsForHash().put("key","f3","333");
        String value = (String) redisTemplate.opsForHash().get("key","f2");
        System.out.println("value:"+value);
        Boolean exists = redisTemplate.opsForHash().hasKey("key","f1");
        System.out.println("exists:"+exists);
        redisTemplate.opsForHash().delete("key","f3","f2");
        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size:"+size);
        return "OK";
    }

    @RequestMapping("/testZSet")
    @ResponseBody
    public String testZSet(){
        redisTemplate.execute((RedisConnection connection)->{
           connection.flushAll();
           return null;
        });
        redisTemplate.opsForZSet().add("key","kzf",88);
        redisTemplate.opsForZSet().add("key","whx",77);
        redisTemplate.opsForZSet().add("key","lyz",85);
        Set<String>members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members:"+members);
        Set<ZSetOperations.TypedTuple<String>>membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key",0,-1);
        System.out.println("membersWithScore:"+membersWithScore);

        Double score = redisTemplate.opsForZSet().score("key","lyz");
        System.out.println("score:"+score);

        redisTemplate.opsForZSet().remove("key","kzf");
        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size:"+size);

        Long rank = redisTemplate.opsForZSet().rank("key", "whx");
        System.out.println("rank:"+rank);

        return "OK";
    }

}
